package io.nplus.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.nplus.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.nplus.modules.generator.entity.DemoEntity;
import io.nplus.modules.generator.service.DemoService;
import io.nplus.common.utils.PageUtils;
import io.nplus.common.utils.R;



/**
 * demo
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-04-02 23:49:55
 */
@RestController
@RequestMapping("generator/demo")
public class DemoController {
    @Autowired
    private DemoService demoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:demo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = demoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:demo:info")
    public R info(@PathVariable("id") Integer id){
			DemoEntity demo = demoService.selectById(id);

        return R.ok().put("demo", demo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:demo:save")
    public R save(@RequestBody DemoEntity demo){
        demo.setCreateTime(new Date());
        demo.setCreateBy(ShiroUtils.getUserEntity().getUsername());
        demo.setLastUpdateBy(ShiroUtils.getUserEntity().getUsername());
        demo.setLastUpdateTime(new Date());
			demoService.insert(demo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:demo:update")
    public R update(@RequestBody DemoEntity demo){
        demo.setLastUpdateBy(ShiroUtils.getUserEntity().getUsername());
        demo.setLastUpdateTime(new Date());
			demoService.updateById(demo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:demo:delete")
    public R delete(@RequestBody Integer[] ids){
			demoService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
